package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysAppMapper;
import com.ruoyi.system.domain.SysApp;
import com.ruoyi.system.service.ISysAppService;

/**
 * 系统应用Service业务层处理
 * 
 * @author ning
 * @date 2024-06-25
 */
@Service
public class SysAppServiceImpl implements ISysAppService 
{
    @Autowired
    private SysAppMapper sysAppMapper;

    @Autowired
    private SysMenuMapper menuMapper;

    /**
     * 查询系统应用
     * 
     * @param appId 系统应用主键
     * @return 系统应用
     */
    @Override
    public SysApp selectSysAppByAppId(Long appId)
    {
        return sysAppMapper.selectSysAppByAppId(appId);
    }

    /**
     * 查询系统应用列表
     * 
     * @param sysApp 系统应用
     * @return 系统应用
     */
    @Override
    public List<SysApp> selectSysAppList(SysApp sysApp)
    {
        return sysAppMapper.selectSysAppList(sysApp);
    }

    /**
     * 新增系统应用
     * 
     * @param sysApp 系统应用
     * @return 结果
     */
    @Override
    public int insertSysApp(SysApp sysApp)
    {
        SysApp app = new SysApp();
        app.setAppCode(sysApp.getAppCode());
        List<SysApp> appList = sysAppMapper.selectSysAppList(app);
        if (appList.size()>0){
            throw new ServiceException("应用编码重复");
        }
        sysApp.setCreateTime(DateUtils.getNowDate());
        return sysAppMapper.insertSysApp(sysApp);
    }

    /**
     * 修改系统应用
     * 
     * @param sysApp 系统应用
     * @return 结果
     */
    @Override
    public int updateSysApp(SysApp sysApp)
    {
        SysApp app = new SysApp();
        app.setAppCode(sysApp.getAppCode());
        List<SysApp> appList = sysAppMapper.selectSysAppList(app);
        for (SysApp entity : appList) {
            if (!entity.getAppId().equals(sysApp.getAppId())){
                throw new ServiceException("应用编码重复");
            }
        }
        sysApp.setUpdateTime(DateUtils.getNowDate());
        return sysAppMapper.updateSysApp(sysApp);
    }

    /**
     * 批量删除系统应用
     * 
     * @param appIds 需要删除的系统应用主键
     * @return 结果
     */
    @Override
    public int deleteSysAppByAppIds(Long[] appIds)
    {
        for (Long id : appIds) {
            SysApp sysApp = sysAppMapper.selectSysAppByAppId(id);
            if (sysApp.getIsDefault() == 2){
                throw new ServiceException("系统默认应用不可删除！");
            }
            SysMenu sysMenu = new SysMenu();
            sysMenu.setAppCode(sysApp.getAppCode());
            List<SysMenu> sysMenus = menuMapper.selectMenuList(sysMenu);
            if (sysMenus.size()>0){
                throw new ServiceException("应用下存在菜单不可删除！");
            }
        }
        return sysAppMapper.deleteSysAppByAppIds(appIds);
    }

    /**
     * 删除系统应用信息
     * 
     * @param appId 系统应用主键
     * @return 结果
     */
    @Override
    public int deleteSysAppByAppId(Long appId)
    {
        return sysAppMapper.deleteSysAppByAppId(appId);
    }
}
